package com.xiaofeng.pochi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.enums.StateEnums;
import com.xiaofeng.pochi.mapper.SysBannerMapper;
import com.xiaofeng.pochi.pojo.LoginUser;
import com.xiaofeng.pochi.pojo.SysBanner;
import com.xiaofeng.pochi.service.SysBannerService;
import com.xiaofeng.pochi.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/08/05/23:00
 * @Description:
 */
@Service
public class SysBannerServiceImpl extends ServiceImpl<SysBannerMapper, SysBanner> implements SysBannerService {

    @Autowired
    private SysBannerMapper sysBannerMapper;

    /**
     * 修改
     *
     * @param sysBanner
     */
    @Override
    public void update(SysBanner sysBanner) {
        LoginUser loginUser = ShiroUtils.getLoginUser();
        sysBanner.setUpdateBy(loginUser.getUsername());
        sysBannerMapper.updateById(sysBanner);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public SysBanner get(Long id) {
        return this.baseMapper.selectById(id);
    }


    /**
     * 启用
     *
     * @param id
     */
    @Override
    public void enable(Long id) {
        SysBanner banner = sysBannerMapper.selectById(id);
        banner.setStatus(StateEnums.ENABLED.getCode());
        sysBannerMapper.updateById(banner);
    }

    /**
     * 禁用
     *
     * @param id
     */
    @Override
    public void disable(Long id) {
        SysBanner banner = sysBannerMapper.selectById(id);
        banner.setStatus(StateEnums.NOT_ENABLE.getCode());
        sysBannerMapper.updateById(banner);
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @Override
    public Page<SysBanner> getByPage(Page<SysBanner> page) {
        List<SysBanner> list = sysBannerMapper.getByPage(page);
        int totalCount = sysBannerMapper.countByPage(page);
        page.setList(list);
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public List<SysBanner> getBannerList() {
        return sysBannerMapper.getBannerList();
    }

    @Override
    public void addClickCount(Long id) {
        sysBannerMapper.updateClickCount(id);
    }
}
